/**
 * Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Copyright [2012-2013] [Guðmundur Björn Birkisson]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ---------------------------------------------------------------------------
 * 
 * Class name:  DebugWindow
 * 
 * Description of class:
 * 
 * Displays assembly code for Morpho VM for debugging.
 * 
 */

package karel.UI;

import java.awt.Color;
import java.awt.Font;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;

public class DebugWindow extends javax.swing.JFrame {

    private static final String KEY_DEBUGWINDOW_X = "debugwindow.x";
    private static final String KEY_DEBUGWINDOW_Y = "debugwindow.y";
    private static final String KEY_DEBUGWINDOW_WIDTH = "debugwindow.width";
    private static final String KEY_DEBUGWINDOW_HEIGHT = "debugwindow.height";
    private final Preferences prefs;
    
    private static Document doc;
    private static SimpleAttributeSet standardText;

    private static boolean debugMode = false;

    /**
     * Simple constructor.
     */
    public DebugWindow() {
        initComponents();
        prefs = Preferences.userNodeForPackage(DebugWindow.class);
        debugTextPane.setFont(new Font("Monospaced", Font.PLAIN, 14));
        setSizeFromPref();
        
        doc = debugTextPane.getDocument();
        
        standardText = new SimpleAttributeSet();
        StyleConstants.setForeground(standardText, Color.BLACK);
        StyleConstants.setBackground(standardText, Color.WHITE);
    }
    
    /**
     * Sets whether debug is on or not.
     * @param debugOn 
     */
    public static void setDebugMode(boolean debugOn) {
        debugMode = debugOn;
    }
    
    /**
     * Gets whether debug is on or not.
     * @return debugMode
     */
    public static boolean debugMode() {
        return debugMode;
    }

    /**
     * Get both location and size last used by user and apply them to the window.
     */
    private void setSizeFromPref() {
        setSize(prefs.getInt(KEY_DEBUGWINDOW_WIDTH, getWidth()), prefs.getInt(KEY_DEBUGWINDOW_HEIGHT, getHeight()));
        setLocation(prefs.getInt(KEY_DEBUGWINDOW_X, getX()), prefs.getInt(KEY_DEBUGWINDOW_Y, getY()));
    }
    
    /**
     * Clears the DebugWindow of any text.
     */
    public static void clearWindow() {
        try {
            doc.remove(0, doc.getLength());
        } catch (BadLocationException ex) {
            Logger.getLogger(DebugWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * Prints input string to the DebugWindow.
     * @param string 
     */
    public static void print(String string) {
        try {
            doc.insertString(doc.getLength(), string, standardText);
        } catch (BadLocationException ex) {
            Logger.getLogger(DebugWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    /**
     * Prints input string to the DebugWindow followed by a new line.
     * @param string 
     */
    public static void println(String string) {
        print(string + "\n");
    }
    
    /**
     * Save both location and size used by user and shut down debugMode;
     */
    public void onClose() {
        setDebugMode(false);
        prefs.putInt(KEY_DEBUGWINDOW_WIDTH, getWidth());
        prefs.putInt(KEY_DEBUGWINDOW_HEIGHT, getHeight());
        prefs.putInt(KEY_DEBUGWINDOW_X, getX());
        prefs.putInt(KEY_DEBUGWINDOW_Y, getY());
    }

    /*
     * Here below is auto generated code from NetBeans IDE 7.2.1
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollPane = new javax.swing.JScrollPane();
        debugTextPane = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Karel - DebugWindow");
        setPreferredSize(new java.awt.Dimension(300, 500));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        scrollPane.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        debugTextPane.setFont(karel.util.FontLoader.getDefaultFont(Font.PLAIN,karel.util.FontLoader.ConsoleFontSize));
        scrollPane.setViewportView(debugTextPane);

        getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Call onClose() on close
     * @param evt 
     */
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        onClose();
    }//GEN-LAST:event_formWindowClosing

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextPane debugTextPane;
    private javax.swing.JScrollPane scrollPane;
    // End of variables declaration//GEN-END:variables
}
